﻿using DataMongo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataMongo.Entities;

namespace MVCMongo.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Company/

        public ActionResult Index()
        {
            return View();
        }
        #region Company

        public ActionResult Company()
        {
            var connection = new RepositoryBaseMongo("Company");
            var data = connection.GetAll<Company>();
            return View(data);

        }
        public ActionResult CompanyAdd()
        {
            var connection = new RepositoryBaseMongo("Company");
            var company = new Company();
            company.ProductServiceCategoryList = new List<SelectListItem>();
            var data = connection.GetAll<Company>();
            company.ProductServiceCategoryList = new SelectList(data, "CompanyId", "CompanyName").ToList();
            company.ProductServiceCategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(company);
        }

        [HttpPost]
        public ActionResult CompanyAdd(Company company)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Company");
                int Id = 0;
                var companies = connection.GetAll<Company>().ToList();
                if (companies.Any())
                    Id = companies.Max(x => x.CompanyId);
                Id += 1;
                company.CompanyId = Id;
                connection.InsertOrUpdate(company);
                return RedirectToAction("Company");
            }
            return null;
        }

        //[HttpPost]
        //public ActionResult CompanyAdd(Company model)
        //{
        //    if(ModelState.IsValid)
        //    {
        //        var connection = new RepositoryBaseMongo("Company");
        //        int Id = 0;
        //        var companies = connection.GetAll<Company>().ToList();
        //        if (companies.Any())
        //            Id = companies.Max(x => x.CompanyID);
        //        Id += 1;
        //        model.CompanyID = Id;
        //        model.isActive = true;
        //        connection.InsertOrUpdate(model);
        //        return RedirectToAction("Company");
        //    }
        //    return null;
        //}
        //public ActionResult CompanyEdit(int id)
        //{
        //    var connection = new RepositoryBaseMongo("Company");
        //    List<Company> list = (from f in connection.GetAll<Company>()
        //                          where f.CompanyID == id
        //                          select f).ToList();
        //    Company company = new Company();
        //    if (list.Count > 0) company = list[0];
        //    var data = connection.GetAll<Company>();
        //    // country.CountryList = new SelectList(data, "CountryId", "Title").ToList();
        //    // country.CountryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
        //    return View(company);
        //}
        //[HttpPost]
        //public ActionResult CompanyEdit(Company model)
        //{

        //    var connection = new RepositoryBaseMongo("Company");
        //    List<Company> list = (from f in connection.GetAll<Company>()
        //                          where f.CompanyID == model.CompanyID
        //                          select f).ToList();
        //    Company newcompany = new Company();
        //    if (list.Count > 0) newcompany = list[0];
        //    model._id = newcompany._id;
        //    connection.InsertOrUpdate(model);
        //    return RedirectToAction("Company");
        //}
        //public ActionResult CompanyDelete(int Id)
        //{
        //    var connection = new RepositoryBaseMongo("Company");
        //    List<Company> list = (from f in connection.GetAll<Company>()
        //                          where f.CompanyID == Id
        //                          select f).ToList();
        //    Company company = new Company();
        //    if (list.Count > 0) company = list[0];
        //    connection.Delete<Company>(company._id);
        //    return RedirectToAction("Company");
        //}

        #endregion Company

        

    }
}
